package com.lottery.im.module.service.ui;

import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageButton;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.redpacket.CanReceiveData2;
import com.lottery.im.api.result.entity.redpacket.Type3SQ;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.adapter.Type3SQListApater;
import com.lottery.im.module.service.presenter.Type3SQListPresenter;
import com.lottery.im.module.service.view.Type3SQListView;
import com.lottery.im.util.ToastUtils;
import com.lottery.im.widget.CustomerLinearLayoutManager;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者：cyy
 * 时间：2018/9/25。
 */
public class Type3SQListFragment extends BaseSwipeFragment<Type3SQListPresenter>
    implements Type3SQListView {
  @BindView(R.id.tv_hourse) TextView tvHourse;
  @BindView(R.id.tv_minute) TextView tvMinute;
  @BindView(R.id.tv_second) TextView tvSecond;
  @BindView(R.id.recycleView) RecyclerView recycleView;
  @BindView(R.id.tv_detail) TextView tvDetail;
  @BindView(R.id.btn_get) ImageButton btnGet;

  private Type3SQListApater mApater;
  private boolean canBrush = true;
  private Handler mHandler = new Handler();
  private Runnable mRunnable;
  private LayoutAnimationController mLayoutAnimation;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_type3sqist;
  }

  @Override protected void initPresenter() {
    presenter = new Type3SQListPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    presenter.getType3SQList();
    presenter.loadCanReceive("2");

    CustomerLinearLayoutManager layoutManager =
        new CustomerLinearLayoutManager(mActivity);
    layoutManager.setScrollEnabled(false);

    mLayoutAnimation = AnimationUtils
        .loadLayoutAnimation(mActivity, R.anim.layout_animation_from_bottom);
    recycleView.setLayoutAnimation(mLayoutAnimation);
    recycleView.setLayoutManager(layoutManager);
    RecyclerViewDivider.with(mActivity).build().addTo(recycleView);
    mApater = new Type3SQListApater();
    recycleView.setAdapter(mApater);

    mHandler.postDelayed(mRunnable = () -> {
      if (canBrush) {
        presenter.getType3SQList();
        mHandler.postDelayed(mRunnable, 8000);
      }
    }, 5000);
  }

  @Override public void loadType3SQSuccess(Type3SQ type3SQ) {
    tvDetail.setText(type3SQ.getPzwb());

    recycleView.setLayoutAnimation(mLayoutAnimation);
    mApater.notifyList(type3SQ.getList());
  }

  long subTimes = 0;
  Handler handlerTime = new Handler();
  Runnable runnableTime;

  @Override public void loadCanReciveData1Success(CanReceiveData2 receiveData2, String returnCode) {
    if(receiveData2==null){
      ToastUtils.showToast(mActivity,"用户未登录");
      return;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      Date startTime = dateFormat.parse(receiveData2.getStartTime());
      Date serviceTime = dateFormat.parse(receiveData2.getServiceTime());
      Date endTime = dateFormat.parse(receiveData2.getEndTime());
      if (serviceTime.getTime() < startTime.getTime()) {//小于开始领时间
        subTimes = startTime.getTime() - serviceTime.getTime();
      } else if (serviceTime.getTime() > startTime.getTime()
          && serviceTime.getTime() < endTime.getTime()) {//可领取时间

      } else if (serviceTime.getTime() > endTime.getTime()) {//大于结束时间，不可领取

      }

      if ("0000".equals(returnCode)) {
        btnGet.setBackgroundResource(R.drawable.btn_ljlq);
        btnGet.setEnabled(true);
      } else if ("0001".equals(returnCode)) {
        btnGet.setBackgroundResource(R.drawable.btn_zbkl);
        btnGet.setEnabled(false);
      } else if ("0002".equals(returnCode)) {
        btnGet.setBackgroundResource(R.drawable.btn_yl);
        btnGet.setEnabled(false);
      }

      handlerTime.postDelayed(runnableTime = () -> {
        if (subTimes > 0) {
          long days = subTimes / (1000 * 60 * 60 * 24);
          long hours = (subTimes - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
          long minutes =
              (subTimes - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
          long seconds =
              (subTimes - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000
                  * 60)) / 1000;
          tvHourse.setText(hours >= 10 ? hours + "" : "0" + hours);
          tvMinute.setText(minutes >= 10 ? minutes + "" : "0" + minutes);
          tvSecond.setText(seconds >= 10 ? seconds + "" : "0" + seconds);
          subTimes = subTimes - 1000 * 1;

          handlerTime.postDelayed(runnableTime, 1000);
        }
      }, 0);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  @Override public void receiveRedPacketSuccess() {
    presenter.loadCanReceive("2");
  }

  @OnClick({ R.id.iv_back, R.id.btn_get }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_get:
        presenter.onReceiveRedPacket("2");
        break;
    }
  }

  @Override public void onStop() {
    super.onStop();
    if (handlerTime != null) {
      handlerTime.removeCallbacks(runnableTime);
    }
    if (mHandler != null) {
      mHandler.removeCallbacks(mRunnable);
    }
    canBrush = false;
  }

}
